package com.f5.management.service.impl;

import com.f5.management.dao.MaintenanceStaffDAO;
import com.f5.management.dao.WarrantyDAO;
import com.f5.management.entity.Equipment;
import com.f5.management.entity.MaintenanceStaff;
import com.f5.management.entity.Warranty;
import com.f5.management.service.WarrantyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class WarrantyServiceImpl implements WarrantyService {
    @Autowired
    private WarrantyDAO warrantyDAO;
    @Autowired
    private MaintenanceStaffDAO staffDAO;

    @Override
    public void updateWarranty(Warranty warranty) {
        warrantyDAO.update(warranty);
    }

    @Override
    public void insertWarranty(Warranty warranty) {
        warrantyDAO.insert(warranty);
    }

    @Override
    public void deleteWarranty(int id) {

    }

    @Override
    public Warranty selectWarranty(int id) {
        return null;
    }

    @Override
    public List<Warranty> list() {
        return warrantyDAO.list();
    }

    @Override
    public int count() {
        return warrantyDAO.count();
    }

    @Override
    @Transactional
    public boolean report(Warranty warranty) {
        // 0--已受理，1--待受理，2--已维修
        int status = warranty.getStatus();
        if (status == 0 || status == 2) {
            return false;
        }
        List<MaintenanceStaff> freeStaffs = staffDAO.freelist();
        if (freeStaffs.size() != 0) {
            MaintenanceStaff staff = freeStaffs.get(0);
            Equipment equipment = warranty.getEquipment();

            warranty.setStatus(0);
            staff.setEquipment(equipment);
            warrantyDAO.update(warranty);
            staffDAO.update(staff);
        } else {
            System.out.println("无空闲修理人员");
            return false;
        }
        return true;
    }
}
